package cn.quartz.dbquartz;

import org.junit.Test;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.concurrent.TimeUnit;

import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

/**
 * ${todo}
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2016/9/17 12:55
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
public class AllPushJobSchedulerTest2 extends BaseServiceTest {
    @Autowired
    SchedulerFactoryBean schedulerFactoryBean;

//    @Before
    public void start() {
        schedulerFactoryBean.start();
    }

    @Test
    public void test() throws SchedulerException, InterruptedException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        scheduler.getListenerManager().addTriggerListener(new MyTriggerListener());
        scheduler.getListenerManager().addJobListener(new MyJobListener());
        JobDetail jobDetail = JobBuilder
                .newJob(TestJob.class)
                .withIdentity("test2", "testGroup")
                .build();
        SimpleTrigger trigger = TriggerBuilder
                .newTrigger()
                .startNow()
//                .endAt(DateBuilder.futureDate(20, DateBuilder.IntervalUnit.SECOND))
                .withIdentity("test2", "testGroup")
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(10)
                        .withRepeatCount(10))
                .build();// 注意，重复10次将总共11触发

        scheduler.scheduleJob(jobDetail,trigger);

//        TimeUnit.SECONDS.sleep(20);
//        scheduler.deleteJob(JobKey.jobKey("test2","testGroup"));

        TimeUnit.SECONDS.sleep(100000);
    }
}